﻿#include "u_userpwd_page.h"

UserPwdPage::UserPwdPage(QWidget *parent) : MyDialog(parent)
{
    this->InitWidget();
    this->SetupConnections();
    this->Initialize();

    this->setTitle("修改密码");
    this->setMaxHide();
    this->resize(350,150);
}

void UserPwdPage::InitWidget(){
    btnEdit = new QPushButton(QChar(0xe657) + QString(" 确定"));
    butCancel  = new QPushButton(QChar(0xe603) + QString(" 取消"));

    editCode = new QLineEdit();     editCode->setEnabled(false);
    editPassword = new QLineEdit(); editPassword->setEchoMode(QLineEdit::Password);
    editName = new QLineEdit();     editName->setEnabled(false);


    QHBoxLayout *btnLayout = new QHBoxLayout();
    btnLayout->addStretch();
    btnLayout->addWidget(btnEdit);
    btnLayout->addWidget(butCancel);

    QGridLayout *layout1 = new QGridLayout();
    layout1->setMargin(10);
    layout1->setSpacing(10);
    layout1->addWidget(new QLabel("账   号"),      1,1,1,1);
    layout1->addWidget(editCode,                 1,2,1,1);
    layout1->addWidget(new QLabel("姓   名"),      2,1,1,1);
    layout1->addWidget(editName,                 2,2,1,1);
    layout1->addWidget(new QLabel("密   码"),      3,1,1,1);
    layout1->addWidget(editPassword,             3,2,1,1);
    layout1->addLayout(btnLayout,                4,1,1,2);

    QGroupBox *groupbox11 = new QGroupBox();
    groupbox11->setStyleSheet("margin-top: 0px;");
    groupbox11->setLayout(layout1);

    //-------------------------------------------------

    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->setMargin(0);
    mainLayout->setSpacing(10);
    mainLayout->addWidget(groupbox11);
    this->addLayout(mainLayout);
}

void UserPwdPage::SetupConnections(){
    connect(btnEdit , SIGNAL(clicked()), this, SLOT(OnBtnEditClicked()));
    connect(butCancel , SIGNAL(clicked()), this, SLOT(OnBtnCancelClicked()));
}

void UserPwdPage::Initialize(){
    User info = globalData->user;
    editCode->setText(info.code);
    editPassword->setText(info.password);
    editName->setText(info.name);
}

void UserPwdPage::OnBtnEditClicked(){
    if(this->editPassword->text().isEmpty()){ editPassword->setFocus(); MyGUI::ShowMessageTip("请输入密码", this); return; }

    if(!MyGUI::ShowMessageBoxQuesion("确定要修改吗？", this)){
        return;
    }

    User info = globalData->user;
    info.password = this->editPassword->text().trimmed();
    bool b = globalData->sqlBase->SaveUser(info);

    if(b){
        MyGUI::ShowMessageBoxInfo("修改成功", this);
    }else{
        MyGUI::ShowMessageBoxError("修改失败", this);
    }
    this->close();
}

void UserPwdPage::OnBtnCancelClicked(){
    this->close();
}
